package com.xmu.prcs.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.xmu.prcs.dao.ReplyDao;
import com.xmu.prcs.entity.Plant;
import com.xmu.prcs.entity.Reply;

/**
 * 
 * @author frequent
 * @version 1.0,2012-3-20
 *
 */
public class ReplyDaoImpl extends GenericDaoImpl<Reply> implements ReplyDao{

	public ReplyDaoImpl() {
		super(Reply.class);
	}

	public int countReply(int postId) {
		String sql = "select count(1) from reply where reply.idpost=" + postId;
		
		Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
		Query query = session.createSQLQuery(sql);
		int size;
		if(query.uniqueResult() == null){
			size = 0;
		}
		size = Integer.parseInt((query.uniqueResult().toString()));
		return size;
	}

	public List<Reply> get4Page(int postId, int begin, int size) {
		List<Reply> list = null;
		String sql = "select * from reply where reply.idpost=" + postId + " order by reply.replytime";

		Query query = getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createSQLQuery(sql).addEntity(Reply.class);
		query.setFirstResult(begin);
		query.setMaxResults(size);
		list = query.list();
		return list;
	}

}
